/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.basics.bean.impl;

import java.io.IOException;

import com.genia.toolbox.basics.bean.Delimiter;
import com.genia.toolbox.basics.exception.technical.TechnicalIOException;

/**
 * This implementation of <code>Delimiter</code> add constant elements.
 */
public class ConstantDelimiter
    implements Delimiter
{

  /**
   * the element to write before the first element of a <code>Collection</code>.
   */
  private final transient String head;

  /**
   * the separator between each element of a <code>Collection</code>.
   */
  private final transient String separator;

  /**
   * the element to write after the last element of a <code>Collection</code>.
   */
  private final transient String tail;



  /**
   * Constructor.
   * 
   * @param head
   *          the element to write before the first element of a
   *          <code>Collection</code>
   * @param separator
   *          the separator between each element of a <code>Collection</code>
   * @param tail
   *          the element to write after the last element of a
   *          <code>Collection</code>
   */
  public ConstantDelimiter(final String head, final String separator, final String tail)
  {
    this.head = head;
    this.separator = separator;
    this.tail = tail;
  }



  /**
   * Append a value before the first element of the <code>Collection</code>.
   * This implementation append <code>head</code>.
   * 
   * @param appendable
   *          The <code>Appendable</code> to which the value is appended
   * @throws TechnicalIOException
   *           when an I/O problem occurred
   */
  public void appendHead(final Appendable appendable)
      throws TechnicalIOException
  {
    appendValue(appendable, head);
  }



  /**
   * Append a value between each element of the <code>Collection</code>. This
   * implementation append <code>separator</code>.
   * 
   * @param appendable
   *          The <code>Appendable</code> to which the value is appended
   * @throws TechnicalIOException
   *           when an I/O problem occurred
   */
  public void appendSeparator(final Appendable appendable)
      throws TechnicalIOException
  {
    appendValue(appendable, separator);
  }



  /**
   * Append a <code>String</code> to an <code>Appendable</code>.
   * 
   * @param appendable
   *          The <code>Appendable</code> to which the value is appended
   * @param value
   *          The <code>String</code> appended.
   * @throws TechnicalIOException
   *           when an I/O problem occurred
   */
  private void appendValue(final Appendable appendable, final String value)
      throws TechnicalIOException
  {
    try {
      appendable.append(value);
    }
    catch (final IOException ioException) {
      throw new TechnicalIOException(ioException);
    }
  }



  /**
   * Append a value after the last element of the <code>Collection</code>.
   * This implementation append <code>tail</code>.
   * 
   * @param appendable
   *          The <code>Appendable</code> to which the value is appended
   * @throws TechnicalIOException
   *           when an I/O problem occurred
   */
  public void appendTail(final Appendable appendable)
      throws TechnicalIOException
  {
    appendValue(appendable, tail);
  }

}
